summaryrefslogtreecommitdiff
path: root/app/[lng]/sales/(sales)/faq/manage/actions.ts
blob: bc443a8a92c2eefc433810bf4cf9f96794fe36d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use server';

import { promises as fs } from 'fs';
import path from 'path';
import { FaqCategory } from '@/components/faq/FaqCard';
import { fallbackLng } from '@/i18n/settings';

const FAQ_CONFIG_PATH = path.join(process.cwd(), 'config', 'faqDataConfig.ts');

export async function updateFaqData(lng: string, newData: FaqCategory[]) {
    try {
        const fileContent = await fs.readFile(FAQ_CONFIG_PATH, 'utf-8');
        const dataMatch = fileContent.match(/export const faqCategories[^=]*=\s*(\{[\s\S]*\});/);
        if (!dataMatch) {
            throw new Error('FAQ 데이터 형식이 올바르지 않습니다.');
        }
        
        const allData = eval(`(${dataMatch[1]})`);
        const updatedData = {
            ...allData,
            [lng]: newData
        };

        const newFileContent = `import { FaqCategory } from "@/components/faq/FaqCard";\n\ninterface LocalizedFaqCategories {\n    [lng: string]: FaqCategory[];\n}\n\nexport const faqCategories: LocalizedFaqCategories = ${JSON.stringify(updatedData, null, 4)};`;
        await fs.writeFile(FAQ_CONFIG_PATH, newFileContent, 'utf-8');
        
        return { success: true };
    } catch (error) {
        console.error('FAQ 데이터 업데이트 중 오류 발생:', error);
        return { success: false, error: '데이터 업데이트 중 오류가 발생했습니다.' };
    }
}

export async function getFaqData(lng: string): Promise<{ data: FaqCategory[] }> {
    try {
        const fileContent = await fs.readFile(FAQ_CONFIG_PATH, 'utf-8');
        const dataMatch = fileContent.match(/export const faqCategories[^=]*=\s*(\{[\s\S]*\});/);
        if (!dataMatch) {
            throw new Error('FAQ 데이터 형식이 올바르지 않습니다.');
        }
        
        const allData = eval(`(${dataMatch[1]})`);
        return { data: allData[lng] || allData[fallbackLng] || [] };
    } catch (error) {
        console.error('FAQ 데이터 읽기 중 오류 발생:', error);
        return { data: [] };
    }
}